﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;
using System.Linq;
using System.Text;
using ihongma.WebData;

namespace ihongma.test
{
   

    public partial class news : System.Web.UI.Page
    {
        JumonyParser parser = new JumonyParser();
        protected void Page_Load(object sender, EventArgs e)
        {
            ////string url = string.Format("http://fo.ifeng.com/dhyana/list_0/{0}.shtml", i);
            //string url = "http://fo.ifeng.com/dhyana/list_0/10.shtml";
            //string html = HttpRequestHelper.GetPageSourceByUrl(url, "utf-8");
            //var dataDocument = parser.Parse(html);
            //var link = dataDocument.Find(".lb_contain01 h1 a");
            //foreach (var item in link)
            //{
            //    Response.Write(item.Attribute("href").Value());
            //}
            string showhtml = HttpRequestHelper.GetPageSourceByUrl("http://fo.ifeng.com/dhyana/detail_2010_05/06/1489802_0.shtml", "utf-8");

           var dataDocument = parser.Parse(showhtml);
           Response.Write(dataDocument.Find("#artical_real p").Count());
           StringBuilder strbb = new StringBuilder();
           string aa = "<img border=\"0\" src=\"http://img.ifeng.com/res/200905/0522_23411.jpg\" alt=\"\">";
           //foreach (var j in dataDocument.Find("#artical_real p").Take(3))
           //{
           //    j.Nodes()

           //    if (j.InnerHtml()!=aa)
           //    {
           //        strbb.AppendFormat("<p>{0}</p>", j.InnerHtml());
           //    }
           //    Response.Write(j.Attribute("src").Value());
           //}
           //Response.Write(parser.Parse(strbb.ToString()).n;
          // Response.Write(dataDocument.Find("#artical_real").Find("[style=text-align: left]").Single().InnerText());
           //Response.Write(dataDocument.Find("#artical_real img").ElementAt(0).Attribute("src").Value());
           //Response.Write(dataDocument.Find("#artical_topic").Single().InnerText().Replace("一日禅：", ""));

        }

        protected void Button1_Click(object sender, EventArgs e)
        {


            for (int i = 13; i<=22; i++)
            {
                string url = string.Format("http://fo.ifeng.com/dhyana/list_0/{0}.shtml", i);
                string html = HttpRequestHelper.GetPageSourceByUrl(url, "utf-8");
                var dataDocument = parser.Parse(html);
                var link = dataDocument.Find(".lb_contain01 h1 a");
                
                    foreach (var item in link)
                    {
                        //Response.Write();
                      
                        string showhtml = HttpRequestHelper.GetPageSourceByUrl(item.Attribute("href").Value(), "utf-8");
                        dataDocument = parser.Parse(showhtml);
                        if (dataDocument.Find("#artical_real p").Count() >= 3)
                        {
                            string temp = string.Empty;
                            StringBuilder strbb = new StringBuilder();
                            strbb.AppendFormat("<p>{0}</p>", dataDocument.Find("#artical_real p").Take(1).Single().InnerHtml());
                            foreach (var j in dataDocument.Find("#artical_real p").Take(3))
                            {
                                //
                                
                                if (j.InnerText().Length > 10)
                                {
                                    temp = j.InnerText();
                                    strbb.AppendFormat("<p>{0}</p>", temp);
                                }
                            }
                            com.ihongma.www.addserver add = new com.ihongma.www.addserver();
                            //temp = ihongma.Common.Utils.DropHTML(dataDocument.Find("#artical_real p").Take(3).Single().InnerHtml());
                            com.ihongma.www.article ar = new com.ihongma.www.article()
                            {
                                add_time = DateTime.Parse(dataDocument.Find("#artical_sth p span").ElementAt(0).InnerText()),
                                author = "colt",
                                category_id = 348,
                                channel_id = 1,
                                zhaiyao = temp,
                                click = 100,
                                content =strbb.ToString().Replace("<img border=\"0\" src=\"http://img.ifeng.com/res/200905/0522_23411.jpg\" alt=\"\">",""),
                                //dataDocument.Find("#artical_real p").ElementAt(0).InnerHtml() + "<p>" + temp + "</p>",

                                title = dataDocument.Find("#artical_topic").Single().InnerText().Replace("一日禅：", ""),
                                digg_act = 0,
                                digg_good = 0,
                                form = "鸿马",
                                img_url = dataDocument.Find("#artical_real img").ElementAt(0).Attribute("src").Value(),
                                is_hot = 0,
                                is_lock = 0,
                                is_msg = 0,
                                is_red = 0,
                                is_slide = 0,
                                is_top = 0,
                                seo_description = temp,
                                seo_keywords = dataDocument.Find("#artical_topic").Single().InnerText().Replace("一日禅：", ""),
                                seo_title = dataDocument.Find("#artical_topic").Single().InnerText().Replace("一日禅：", ""),
                                sort_id = 99,
                                link_url = ""
                            };
                            add.addnews(ar, "s123456");
                        }
                    }
                
            }
            //string html = HttpRequestHelper.GetPageSourceByUrl("http://www.ifanr.com/category/special/info-graph", "utf-8");
            //var dataDocument = parser.Parse(html);
            //var link = dataDocument.Find(".entry-header h2 a");
            //var img = dataDocument.Find(".post-thumb img");
            //foreach (var item in img)
            //{
            //    string str = item.Attribute("src").Value();
            //    Response.Write(str);
            //}
        }
    }
}